package com.bookmanger.server.mapper;

import com.bookmanger.pojo.Entity.BookEntity;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface BookMapper {
    @Select("SELECT * FROM book WHERE id = #{id}")
    BookEntity selectById(Integer id);
    
    @Select("SELECT * FROM book WHERE isbn_id = #{isbnId} AND is_delete = 0")
    List<BookEntity> selectByIsbnId(Integer isbnId);
    
    @Insert("INSERT INTO book(book_number, isbn_id, is_delete) VALUES(#{bookNumber}, #{isbnId}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BookEntity bookEntity);

    @Update("UPDATE book SET is_delete = 1 WHERE id = #{id}")
    int deleteById(Integer id);

    @Update("UPDATE book SET is_delete = 1 WHERE book_number = #{bookNumber}")
    int deleteByBookNumber(String bookNumber);
    
    @Update("UPDATE book SET is_delete = 1 WHERE isbn_id = #{isbnId}")
    int deleteByIsbnId(Integer isbnId);

    @Select("SELECT * FROM book WHERE book_number = #{bookNumber} AND is_delete = 0")
    BookEntity selectByBookNumber(String bookNumber);

    @Select("SELECT * FROM book WHERE isbn_id = #{isbnId} AND is_delete = 0 AND is_pass=0 LIMIT 1")
    BookEntity findAvailableBookByIsbnId(Integer isbnId);
    
    @Update("UPDATE book SET is_pass = #{isPass} WHERE id = #{id}")
    int updateBookPassStatus(@Param("id") Integer id, @Param("isPass") Integer isPass);
}